﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace 图片合并
{
    class Program
    {
        static void Main(string[] args)
        {
            CombineTwo();
            Console.WriteLine("OK");

        }

#pragma warning disable CA1416 // 验证平台兼容性
        public static void Combine()
        {
            Bitmap bg = new("bg.jpg");

            DirectoryInfo directory = new("Src");
            foreach(var file in directory.GetFiles())
            {
                Bitmap src = new("Src/"+file.Name);
                Bitmap tgt = new(bg);

                int left = (bg.Width - src.Width) / 2;
                int top = (bg.Height - src.Height) / 2;

                for(int i = 0; i < src.Width; i++)
                {
                    for(int j = 0; j < src.Height; j++)
                    {
                        tgt.SetPixel(i + left, j + top, src.GetPixel(i, j));
                    }
                }

                tgt.Save("Tgt/" + file.Name.Split("/").ToList().Last().Replace("png","jpg"),ImageFormat.Jpeg);

                src.Dispose();
                tgt.Dispose();

                file.Delete();
            }

        }
#pragma warning restore CA1416 // 验证平台兼容性


#pragma warning disable CA1416 // 验证平台兼容性
        public static void CombineTwo()
        {
            Bitmap bg = new(1024,768);

            DirectoryInfo directory = new("Src");
            foreach (var file in directory.GetFiles())
            {
                Bitmap src = new("Src/" + file.Name);
                Bitmap tgt = new(bg);

                int left = (bg.Width - src.Width) / 2;
                int top = (bg.Height - src.Height) / 2;

                for (int i = 0; i < src.Width; i++)
                {
                    for (int j = 0; j < src.Height; j++)
                    {
                        tgt.SetPixel(i + left, j + top, src.GetPixel(i, j));
                    }
                }

                tgt.Save("Tgt/" + file.Name.Split("/").ToList().Last().Replace("png", "jpg"), ImageFormat.Jpeg);

                src.Dispose();
                tgt.Dispose();

                file.Delete();
            }

        }
#pragma warning restore CA1416 // 验证平台兼容性
    }
}
